import { IpcOn, Window } from '@doubleshot/nest-electron'
import { Ret } from '@main/common/utils/ret'
import { Controller } from '@nestjs/common'
import { Payload } from '@nestjs/microservices'
import { BrowserWindow } from 'electron'
import { CreateUserDTO } from './dto/create-user.dto'
import { UserService } from './user.service'

@Controller()
export class UserController {
  constructor(private readonly userService: UserService, @Window() private readonly mainWin: BrowserWindow) {}

  @IpcOn('user/create')
  async create(@Payload() dto: CreateUserDTO) {
    const { webContents } = this.mainWin
    await this.userService.create(dto)
    webContents.send('user/create:res', await Ret.ok())
  }

  @IpcOn('user/list')
  async list() {
    const { webContents } = this.mainWin
    webContents.send('user/list:res', await Ret.ok(this.userService.list()))
  }

  @IpcOn('user/delete')
  async delete(@Payload() id: string) {
    const { webContents } = this.mainWin
    await this.userService.delete(id)
    webContents.send('user/delete:res', await Ret.ok())
  }
}
